<?php

namespace app\api\service\weixinApp;

use app\admin\model\Rechargerecord;
use think\Cache;
use think\Db;

class RechargePay extends BasePay
{
    public function pay($param)
    {
        if (empty($param->total_fee)){
            return show(config('status.ERROR_STATUS'),'缺少参数','充值金额未接收');
        }
        if (empty($param->memberId)){
            return show(config('status.ERROR_STATUS'),'缺少参数','用户Id未接收');
        }
        if (empty($param->type)){
            return show(config('status.ERROR_STATUS'),'缺少参数','支付类型未接收');
        }
        if (!in_array($param->type, [1, 2])) {
            return show(config('status.ERROR_STATUS'), '缺少参数', '支付方式(type) 不正确');
        }
        Db::startTrans();
        //创建订单
        $id = create_order_sn();
        $data['record_money'] = $param->total_fee;
        $data['member_id']  = $param->memberId;
        $data['pay_method'] = $param->type;
        $data['order_no'] = $id;
        $data['gift_money'] = $param->gift_money;
        $data['type'] = 1;
        $data['detail'] = '充值 ¥'.$data['record_money'] /100;

        $result = Rechargerecord::create($data,true);
        if ($result === false){
            Db::rollback();
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, '创建订单失败');
        }
        //调用微信支付
        if ($param->type == 1){
            $openid = '';
            $productName = '秒讯科技';
            $pay_back_url = config('wx.recharge_pay_back_url');
            Db::commit();
            return show(config('status.SUCCESS_STATUS'),'ok',$this->run($param->total_fee, $productName, $openid, $id, $pay_back_url));
        }else if ($param->type == 2){
            //调用支付宝支付
            Db::commit();
        }else{
            Db::rollback();
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, '错误的传参');
        }
    }
}